﻿namespace IBTrader.Indicator.Oscillator
{
    using IBTrader.Indicator.Average;
    using IBTrader.Modules.Read;
    using IBTrader.Charts;
    using System;

    /// <summary>
    /// The Percentage Price Oscillator (PPO) is a momentum oscillator that measures the difference between two moving averages as a percentage of the larger moving average. 
    /// As with its cousin, MACD, the Percentage Price Oscillator is shown with a signal line, a histogram and a centerline. 
    /// Signals are generated with signal line crossovers, centerline crossovers and divergences. 
    /// First, PPO readings are not subject to the price level of the security. 
    /// Second, PPO readings for different securities can be compared, even when there are large differences in the price. 
    /// </summary>
    class PPO : XEMA
    {
        private Line line1 = null;
        public PPO(Worker worker, int interval1 = 9, int interval2 = 12, int interval3 = 26) : base(worker, new ChartArgs { Group = 1 }, interval1, interval2, interval3) { }
        protected override void Init()
        {
            emas[1].Hit += (_, line) => line1 = line;
            emas[2].Hit += (sender, line) => emas[0].Add(sender, NewLine(line, ((line1.Price - line.Price) / line.Price) * 100));
            emas[0].Hit += (_, line) => OnHit(line, line.Price);
        }
        public override void Add(object sender, Line line)
        {
            emas[1].Add(sender, line);
            emas[2].Add(sender, line);
        }
    }
}
